//
//  SleepDataLineChartView.swift
//  MoyoungHome
//
//  Created by 李然 on 2022/9/26.
//

import UIKit

import RxSwift
import RxCocoa
import MYModule


open class SleepDataLineChartView: View {
    
    public init() {
        super.init(frame: .zero)
    }
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   
    open override func makeUI() {
        super.makeUI()
        
        self.backgroundColor = UIColor.background
        
        let dataStack = StackView()
        dataStack.axis = .horizontal
        dataStack.alignment = .firstBaseline
        dataStack.contentMode = .center
        dataStack.addArrangedSubview(hourLabel)
        dataStack.addArrangedSubview(hourUnitLabel)
        dataStack.addArrangedSubview(minuteLabel)
        dataStack.addArrangedSubview(minuteUnitLabel)
        
        
        let labelStackView = StackView()
        labelStackView.axis = .vertical
        labelStackView.alignment = .center
        
        self.addSubview(labelStackView)
        labelStackView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(10)
            make.centerX.equalToSuperview()
        }
       
        labelStackView.addArrangedSubview(dataStack)
        labelStackView.addArrangedSubview(timeLabel)
        
        let lineTipStack = StackView()
        lineTipStack.axis = .horizontal
        lineTipStack.alignment = .center
        lineTipStack.distribution = .equalSpacing
        lineTipStack.spacing = 20.0 * UIScreen.main.bounds.size.width/375.0
        let lineTipArray:[(color:UIColor,title:String)] = [(color: UIColor.Sleep.restful, title:"sleep_restful".language),
                                                           (color:UIColor.Sleep.light, title:"sleep_light".language),
                                                           (color:UIColor.Sleep.rem, title: "sleep_eye_movement".language),
                                                           (color: UIColor.Sleep.awake, title:"sleep_awake".language)]
        
        for config in lineTipArray {
            let lineTipView = SleepLineTipView(color: config.color, title: config.title)
            lineTipStack.addArrangedSubview(lineTipView)
        }
        
        self.addSubview(lineTipStack)
        lineTipStack.snp.makeConstraints { make in
            make.top.equalTo(labelStackView.snp.bottom).offset(26)
            make.leading.equalToSuperview().offset(30)
            make.trailing.equalToSuperview().offset(-30)
        }
        
        self.addSubview(barChartView)
        barChartView.snp.makeConstraints { make in
            make.top.equalTo(lineTipStack.snp.bottom).offset(20)
            make.centerX.equalToSuperview()
            make.width.equalTo(kScreenWidth - 40)
            make.height.equalTo(130)
        }
        
        self.addSubview(startTimeLabel)
        startTimeLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(20)
            make.top.equalTo(barChartView.snp.bottom).offset(5)
            
        }
        
        self.addSubview(endTimeLabel)
        endTimeLabel.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-20)
            make.centerY.equalTo(startTimeLabel)
            make.bottom.equalToSuperview().offset(-20)
        }
    }
    
    func bind(by viewModel:SleepDataChartViewModel) {
        let input = SleepDataChartViewModel.Input(selectIndex: self.barChartView.selectIndex)
        let output = viewModel.transform(input: input)
        output.timeString.drive(self.timeLabel.rx.text).disposed(by: rx.disposeBag)
        output.hourString.drive(self.hourLabel.rx.text).disposed(by: rx.disposeBag)
        output.minuteString.drive(self.minuteLabel.rx.text).disposed(by: rx.disposeBag)
        
    
        
        let lineDataLists = viewModel.defaultValue.detail.map { detail -> SegmentLineChartModel in
            
            // 快速眼动 绘制在第二行，而 detail.type = 3,所以
            // 手动调整位置
            
            // TODO: sleep TODO
            
            var type = detail.type
            
             if detail.type == 1 {
                type = 2
            } else if detail.type == 2 {
                type = 3
            } else if detail.type == 3 {
                type = 1
            }
            
            return SegmentLineChartModel(total: Double(detail.total), type: type)
        }
        self.barChartView.data = lineDataLists
        
        let startTimeString = viewModel.defaultValue.fallAsleepDate?.toString(.custom("MM/dd HH:mm")) ?? "--"
        self.startTimeLabel.text = "sleep_fail_asleep".language(with: [startTimeString])
        
        let endTimeString = viewModel.defaultValue.awakeDate?.toString(.custom("MM/dd HH:mm")) ?? "--"
            self.endTimeLabel.text = "sleep_wake_up".language(with: [endTimeString])
        
    }
    
    func slider(_ point:CGPoint ,inView view:UIView){
        self.barChartView.setTouchLocation(point, inView: view)
    }
    
    public func clearHighlight() {
        
        self.barChartView.clearHighlight()
    }
    
    lazy var hourLabel: Label = {
        let label = Label.init()
        label.font = Fonts.boldSystemFont(ofSize: 40)
        label.textColor = UIColor.assist_222222
        return label
    }()
    
    lazy var hourUnitLabel: Label = {
        let label = Label.init()
        label.font = Fonts.systemFont(ofSize: 11)
        label.textColor = UIColor.assist_222222
        label.text = "sleep_hour".language
        return label
    }()
    
    lazy var minuteLabel: Label = {
        let label = Label.init()
        label.font = Fonts.boldSystemFont(ofSize: 40)
        label.textColor = UIColor.assist_222222
        return label
    }()
    
    lazy var minuteUnitLabel: Label = {
        let label = Label.init()
        label.font = Fonts.systemFont(ofSize: 11)
        label.textColor = UIColor.assist_222222
        label.text = "sleep_min".language
        return label
    }()
    
    lazy var timeLabel: Label = {
        let label = Label.init()
        label.font = Fonts.systemFont(ofSize: 11)
        label.textColor = UIColor.assist_222222
        return label
    }()
    
    lazy var barChartView: SegmentLineChartView = {
        let view = SegmentLineChartView.init(frame:.zero)
        view.highlightEnable = true
        view.colors = UIColor.Sleep.sleep_gradual_change
        return view
    }()
    
    lazy var startTimeLabel: Label = {
        let label = Label.init()
        label.font = Fonts.systemFont(ofSize: 11)
        label.textColor = UIColor.assist_D0D0D0
        return label
    }()
    
    lazy var endTimeLabel: Label = {
        let label = Label.init()
        label.font = Fonts.systemFont(ofSize: 11)
        label.textColor = UIColor.assist_D0D0D0
        return label
    }()
  
}

